package com.igeek.javase.ch04.gameTest;

/*
 *@author wangpeng
 *2023/7/13 19:30
 *@description TODO
 */

/*
* 1、定义一个英雄类Hero，可以通过这个类创建游戏角色，
游戏角色拥有昵称name，等级level，血量blood，攻击力power，防御力defence，
种族kind，装备Equip（只能装3个装备，初始没有装备）。
游戏角色拥有基本攻击方法（拥有一定几率打出暴击）attack(Monster m)，
死亡判定方法isDead()，添加装备的方法addEquip(Equip e)，
获取角色信息的方法show()。
* */

public class Hero extends Role{

        //一个英雄可以有三个装备  关联关系：一对多
        private Equipments[] equipments = new Equipments[3];
        //统计装备数量
        private int index;

        public Hero(String nickName, int level, int blood, int power, int defence, String kind) {
            super(nickName, level, blood, power, defence, kind);
        }
        //重写死亡方法
        @Override
        public boolean isDead() {
            if (super.getBlood() <= 0) {
                System.out.println("当前英雄已阵亡！");
                return true;
            }
            return false;
        }

        //重写攻击方法
        @Override
        public void attack(Role monster) {
            //有一定几率打出暴击率
            int n = (int) (Math.random() * 10 + 1);
            int block = 0;
            if (n >= 3) {
                block = n * 3;
            }
            //怪兽的失血量 = 英雄攻击力 + 暴击率 - 怪兽的防御力
            int blood = (super.getPower() + block - monster.getDefence());
            if (blood > 0) {
                monster.setBlood(monster.getBlood() - blood);
                System.out.println("当前怪兽失血量为：" + blood + "，剩余血量：" + monster.getBlood());
            } else {
                System.out.println("此回合攻击无效");
            }
        }

        //添加装备的方法
        public void addEquip(Equipments e) {
            if (index >= 3) {
                System.out.println("装备已满！");
                return;
            }
            equipments[index++] = e;
            //添加装备，合并装备的攻击力
            //this.power += e.getPower();
            super.setPower(super.getPower()+e.getPower());
            //添加装备，合并装备的防御力
            //this.defence += e.getDefence();
            super.setDefence(super.getDefence()+e.getDefence());
        }

        //显示信息的方法
        public void show() {
            System.out.println("英雄的昵称：" + super.getNickName());
            System.out.println("英雄的等级：" + super.getLevel());
            System.out.println("英雄的血量：" + super.getBlood());
            System.out.println("英雄的攻击力：" +super.getBlood());
            System.out.println("英雄的防御力：" + super.getDefence());
            System.out.println("英雄的类别：" + super.getKind());

            System.out.println("---------英雄装备的信息----------");
            if (equipments != null) {
                for (int i = 0; i < equipments.length; i++) {
                    if (equipments[i] != null) {
                        equipments[i].show();
                    }
                }
            }
        }

        public Hero(String nickName, int level, int blood, int power, int defence, String kind, Equipments[] equipments) {
            super.setNickName(nickName);
            super.setLevel(level);
            super.setBlood(blood);
            super.setPower(power);
            super.setDefence(defence);
            super.setKind(kind);
            this.equipments = equipments;
        }

        /**
         * 获取
         *
         * @return equipments
         */
        public Equipments[] getEquipments() {
            return equipments;
        }

        /**
         * 设置
         *
         * @param equipments
         */
        public void setEquipments(Equipments[] equipments) {
            this.equipments = equipments;
        }

        public String toString() {
            return "Hero{nickName = " + super.getNickName() + ", level = " + super.getLevel() + ", blood = " + super.getBlood() + ", power = " + super.getPower() + ", defence = " + super.getDefence() + ", kind = " + super.getKind() + ", equipments = " + equipments + "}";
        }
}
